﻿@model AdminIndexViewModel
@using Orchard.Environment.Shell.Models
@using Orchard.Environment.Shell

@functions {
    public string GetTenantUrl(ShellSettings tenantShellSettings)
    {
        var requestHostInfo = Context.Request.Host;

        var tenantUrlHost = tenantShellSettings.RequestUrlHost?.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).First() ?? requestHostInfo.Host;
        if (requestHostInfo.Port.HasValue)
        {
            tenantUrlHost += ":" + requestHostInfo.Port;
        }

        var result = $"{Context.Request.Scheme}://{tenantUrlHost}";

        if (!string.IsNullOrEmpty(tenantShellSettings.RequestUrlPrefix))
        {
            result += "/" + tenantShellSettings.RequestUrlPrefix;
        }

        return result;

    }
}

<h1>@RenderTitleSegments(T["Tenants"])</h1>

<nav class="admin-toolbar">
    <ul class="navbar-nav mr-auto"></ul>
    <ul class="navbar-nav">
        <li class="nav-item">
            <a asp-route-action="Create" class="btn btn-primary" role="button">@T["Add Tenant"]</a>
        </li>
    </ul>
</nav>

<form asp-action="Index">
    @* the form is nessecary to generate and antiforgery token for the delete action *@
    <ul class="list-group">
        @foreach (var entry in Model.ShellSettingsEntries)
        {
            <li class="list-group-item">
                <div class="properties">
                    <div class="related">
                        <a asp-action="Edit" asp-route-id="@entry.Name" class="btn btn-primary btn-sm">@T["Edit"]</a>
                        @if (!entry.IsDefaultTenant && entry.ShellSettings.State == TenantState.Running)
                        {
                            <a asp-action="Disable" asp-route-id="@entry.Name" class="btn btn-warning btn-sm" itemprop="UnsafeUrl">@T["Disable"]</a>
                        }
                        @if (entry.ShellSettings.State == TenantState.Disabled)
                        {
                            <a asp-action="Enable" asp-route-id="@entry.Name" class="btn btn-success btn-sm" itemprop="UnsafeUrl">@T["Enable"]</a>
                        }
                        @if (entry.ShellSettings.State == TenantState.Uninitialized)
                        {
                            <a class="btn btn-success btn-sm" href="@GetTenantUrl(entry.ShellSettings)">@T["Setup"]</a>
                        }
                        <a asp-action="Reload" asp-route-id="@entry.Name" class="btn btn-secondary btn-sm" itemprop="UnsafeUrl">@T["Reload"]</a>
                    </div>
                    <h5>
                        <a href="@GetTenantUrl(entry.ShellSettings)">@entry.Name</a>
                        <code class="hint">@entry.ShellSettings.RequestUrlHost/@entry.ShellSettings.RequestUrlPrefix</code>
                        @switch (entry.ShellSettings.State)
                        {
                            case TenantState.Disabled:
                                <span class="badge badge-default">@T["Disabled"]</span>
                                break;
                            case TenantState.Initializing:
                                <span class="badge badge-Info">@T["Initializing"]</span>
                                break;
                            case TenantState.Invalid:
                                <span class="badge badge-warning">@T["Invalid"]</span>
                                break;
                            case TenantState.Running:
                                <span class="badge badge-success">@T["Running"]</span>
                                break;
                            case TenantState.Uninitialized:
                                <span class="badge badge-primary">@T["Uninitialized"]</span>
                                break;
                        }
                    </h5>
                </div>
            </li>
        }
    </ul>
</form>

